<template>
	<view>
		<view class="fixed">
			<view :isBack="true" bgColor="bg-shadeTop text-white">
				<block slot="backText">返回</block>
				<block slot="content">垂直导航</block>
			</view>
		</view>
		<swiper class="screen-swiper round-dot" :indicator-dots="true" :circular="true" :autoplay="true" interval="5000"
			duration="500">
			<swiper-item v-for="(item,index) in 1" :key="index">
				<image
					src="/static/bj.png"
					mode="aspectFill"></image>
			</swiper-item>
		</swiper>
		<view class="VerticalBox">
			<scroll-view class="VerticalNav nav" scroll-y scroll-with-animation :scroll-top="verticalNavTop"
				style="height:calc(100vh - 375upx)">
				<view class="cu-item" :class="index==tabCur?'text-green cur':''" v-for="(item,index) in list"
					:key="index" @tap="TabSelect" :data-id="index">
					{{item.stName}}
				</view>
			</scroll-view>
			<scroll-view class="VerticalMain" scroll-y scroll-with-animation style="height:calc(100vh - 375upx)"
				:scroll-into-view="'main-'+mainCur" @scroll="VerticalMain">
				<view class="padding-top padding-lr" v-for="(item,index) in list" :key="index" :id="'main-'+index">
					<view class="cu-bar solid-bottom bg-white">
						<view class="action">
							<text class="cuIcon-title text-green"></text>{{item.stName}}
						</view>
					</view>
					<view class="cu-list menu-avatar">

						<view class="cu-item" v-for="items in item.shops" @click="pushPlay(items.spId)">
							<view class="cu-avatar round lg" :style="`background-image:url(${items.spImg});`">
							</view>
							<view class="content">
								<view class="text-grey">
									<text class="text-cut">{{items.spName}}</text>
									<view class="cu-tag  bg-red sm" style="right: -50%;position: absolute;">
										{{items.spType}}
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url: this.$url+"/frontend",
				list: [],
				tabCur: 0,
				mainCur: 0,
				verticalNavTop: 0,
				load: true
			};
		},
		onLoad() {
			uni.showLoading({
				title: '加载中...',
				mask: true
			});
			uni.request({
				url: this.url + "/sort/all", //仅为示例，并非真实接口地址。
				method: "GET",
				success: (res) => {
					if (res.data.code == 200) {
						//list = res.data.data.data
						this.list = res.data.data.data
						this.listCur = 0;
					}
				}
			});
		},
		onReady() {
			uni.hideLoading()
		},
		methods: {
			pushPlay(spId) {
				uni.navigateTo({
					url: "../index/index?spId=" + spId
				})
			},
			TabSelect(e) {
				//console.log(e)
				this.tabCur = e.currentTarget.dataset.id;
				this.mainCur = e.currentTarget.dataset.id;
				this.verticalNavTop = (e.currentTarget.dataset.id - 1) * 50
			},
			VerticalMain(e) {
				// #ifdef MP-ALIPAY
				return false //支付宝小程序暂时不支持双向联动 
				// #endif
				let that = this;
				let tabHeight = 0;
				if (this.load) {
					for (let i = 0; i < this.list.length; i++) {
						let view = uni.createSelectorQuery().select("#main-" + this.list[i].id);
						view.fields({
							size: true
						}, data => {
							this.list[i].top = tabHeight;
							//console.log(this.list[i])
							tabHeight = tabHeight + this.list[i].stId * 50;
							this.list[i].bottom = tabHeight;
						}).exec();
					}
					this.load = false
				}
				let scrollTop = e.detail.scrollTop + 10;
				if (scrollTop == 10) {
					this.verticalNavTop = 0
					this.tabCur = 0
					return
				}
				//console.log(scrollTop)
				for (let i = 0; i < this.list.length; i++) {
					if (scrollTop > this.list[i].top && scrollTop < this.list[i].bottom) {
						this.verticalNavTop = (this.list[i].stId - 1) * 50
						//console.log(this.list[i].stId)
						this.tabCur = this.list[i].stId
						return false
					}
				}
			}
		},
	}
</script>

<style scoped>
	.fixed {
		position: fixed;
		z-index: 99;
	}

	.VerticalNav.nav {
		width: 200upx;
		white-space: initial;
	}

	.VerticalNav.nav .cu-item {
		width: 100%;
		text-align: center;
		background-color: #fff;
		margin: 0;
		border: none;
		height: 50px;
		position: relative;
	}

	.VerticalNav.nav .cu-item.cur {
		background-color: #f1f1f1;
	}

	.VerticalNav.nav .cu-item.cur::after {
		content: "";
		width: 8upx;
		height: 30upx;
		border-radius: 10upx 0 0 10upx;
		position: absolute;
		background-color: currentColor;
		top: 0;
		right: 0upx;
		bottom: 0;
		margin: auto;
	}

	.VerticalBox {
		display: flex;
	}

	.VerticalMain {
		background-color: #f1f1f1;
		flex: 1;
	}
</style>
